package cn.eclass.dao;

import cn.eclass.entity.Entity;
import cn.eclass.entity.GiftCategoryEntity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GiftCategoryDao extends CommonDao {

    private static String TABLE_NAME = "giftcategory";

    public GiftCategoryDao() {
        super(GiftCategoryDao.class.getName());
    }

    public List<GiftCategoryEntity> getList() {
        List<GiftCategoryEntity> giftCategories = new ArrayList<GiftCategoryEntity>();
        String sql = "select * from " + TABLE_NAME;
        giftCategories = (List<GiftCategoryEntity>) select(sql);
        return giftCategories;
    }

    public GiftCategoryEntity select(int theId) {
        GiftCategoryEntity entity = null;
        String sql = "select * from " + TABLE_NAME + " where id = " + theId;
        List<GiftCategoryEntity> entities = (List<GiftCategoryEntity>) super.select(sql);
        if (entities.size() > 0) {
            entity = entities.get(0);
        }
        return entity;
    }

    public boolean delete(int giftCatId) {
        String sql = "delete from " + TABLE_NAME + " where id = " + giftCatId;
        int effectedRows = super.delete(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean update(GiftCategoryEntity giftCategoryEntity) {
        String sql = "update " + TABLE_NAME + " set "
                + "`NAME` = '" + giftCategoryEntity.getName() + "',"
                + "`DESCRIPTION` = '" + giftCategoryEntity.getDescription() + "'"
                + "`ORDER` = '" + giftCategoryEntity.getOrder() + "'"
                + " where ID = " + giftCategoryEntity.getId();
        int effectedRows = super.delete(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public GiftCategoryEntity add(GiftCategoryEntity entity) {
        String sql = "insert into " + TABLE_NAME
                + " (`NAME`,`DESCRIPTION`,`ORDER`)"
                + " values "
                + " ("
                + "'" + entity.getName() + "',"
                + "'" + entity.getDescription() + "'"
                + "'" + entity.getOrder() + "'"
                + ")";
        int lastId = super.insert(sql);
        entity.setId(lastId);
        return entity;
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {

        GiftCategoryEntity giftCategoryEntity = null;
        try {
            giftCategoryEntity = new GiftCategoryEntity();
            giftCategoryEntity.setId(resultSet.getInt("ID"));
            giftCategoryEntity.setName(resultSet.getString("NAME"));
            giftCategoryEntity.setDescription(resultSet.getString("DESCRIPTION"));
            giftCategoryEntity.setOrder(resultSet.getInt("ORDER"));

        } catch (SQLException ex) {
            logger.error(ex);
        }
        return giftCategoryEntity;
    }
}
